package media.opensync;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.SortedSet;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class AdaptorMarked extends BaseAdapter {
	
	private Activity activity;
	private ArrayList<TextView> files = new ArrayList<TextView>();

	public AdaptorMarked(Activity activity)
	{
		this.activity = activity;
	}

	@Override
	public View getView (int position, View convertView, ViewGroup list) 
	{		
		View element;

		if (convertView == null) 
		{
			LayoutInflater inflater = activity.getLayoutInflater();
			element = inflater.inflate(R.layout.list2, null);			
			
			TextView file =(TextView)element.findViewById(R.id.file); 
			element.setTag(file); 
		} else {
			element = convertView;
		}
		
		TextView file = (TextView)element.getTag();

		file.setText(files.get(position).getText());

		return element;
	}

	@Override
	public int getCount ()
	{
		return files.size ();
	}

	@Override
	public Object getItem(int position) 
	{
		return files.get(position);
	}

	@Override
	public long getItemId(int position) 
	{
		return position;
	}
	
	public void addFiles (SortedSet<String> filesMarked){
		Iterator<String> it = filesMarked.iterator();
		while(it.hasNext()){
			String path = it.next();
			TextView file = new TextView(activity);
			file.setText(path);
			files.add(file);
			this.notifyDataSetChanged();
		}
	}
	
	public void delItem (int pos) { 
		DirList.filesToSend.remove(files.get(pos).getText().toString());
		files.remove(pos);
		this.notifyDataSetChanged();
	}
}
